package com.example.fish.controller;

import com.example.fish.pojo.User;
import com.example.fish.service.WebSocketService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.ResultMessage;
import com.example.fish.vo.UserChatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController extends BaseController{
    @Autowired
    private WebSocketService webSocketService;

//    初始化时查询聊天记录
    @PostMapping("/history")
    private JsonResult<List<ResultMessage>> getList(@RequestParam("targetuserid") Integer targetUserid, Integer fromuserid){
//        User user =(User) session.getAttribute("user");
        List<ResultMessage> firstTalkMessages = webSocketService.isFirstTalk(fromuserid, targetUserid);
        return new JsonResult<>(200,"success",firstTalkMessages);
    }
//    得到全部聊天用户的数据
    @GetMapping("/users")
    private JsonResult<List<UserChatInfo>> getusers(HttpSession session){
        User user =(User) session.getAttribute("user");
        List<UserChatInfo> chatUser = webSocketService.getChatUser(user.getUserid());
        return new JsonResult<>(200,"success",chatUser);
    }
}
